package src.origintrail;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author csmythe
 */
public class RiverFrame extends JFrame{
    private static JLabel inst;
    private static JButton toll;
    private static JButton ford;
    private static JButton goAround;
    
    public RiverFrame(){
    }
    
    public void init(){
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        inst = new JLabel("You have come to a river");
        toll = new JButton("Pay toll (200 money)");
        toll.addActionListener(new RListener(this));
        ford = new JButton("Ford river");
        ford.addActionListener(new RListener(this));
        goAround = new JButton("Go around (50 food)");
        goAround.addActionListener(new RListener(this));
        this.add(inst);
        this.add(toll);
        this.add(ford);
        this.add(goAround);
        this.pack();
        this.setVisible(true);
    }

    private static class RListener implements ActionListener {
        RiverFrame parent;

        public RListener(RiverFrame r) {
            parent = r;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource().equals(toll)){
                if(OriginTrail.plr.bags.find(new Money()).getStack() >= 200){
                        OriginTrail.plr.bags.addItem(new Money().initialize(-200));
                        OriginTrail.plr.getMap().moveOne();
                        ModuleMove.redoLabels();
                        parent.dispose();
                }else{
                    inst.setVisible(false);
                    inst.setText("Not enough money!");
                    inst.setVisible(true);
                    parent.pack();
                }
            }else if(e.getSource().equals(ford)){
                Random r = new Random();
                int test = r.nextInt(100);
                if(test < 50){
                        int fcost = Math.max(-r.nextInt(14)-14, -OriginTrail.plr.bags.find(new Food()).getStack());
                        int ocost = Math.max(-r.nextInt(2)-1, -OriginTrail.plr.bags.find(new Oxen()).getStack());
                        int ccost = Math.max(-r.nextInt(4), -OriginTrail.plr.bags.find(new Clothing()).getStack());
                        OriginTrail.plr.bags.addItem(new Food().initialize((int)(fcost)));
                        OriginTrail.plr.bags.addItem(new Oxen().initialize((int)(ocost)));
                        OriginTrail.plr.bags.addItem(new Clothing().initialize((int)(ccost)));
                        ModuleMove.renote("You lost some supplies! Food: " + -fcost+", oxen: " + -ocost + ", clothing: " + -ccost+".");
                }else if(test < 75){
                        int rem = r.nextInt(OriginTrail.plr.getParty().length);
                        PartyMember[] temp = new PartyMember[OriginTrail.plr.getParty().length-1];
                        System.out.println (rem);
                        for(int i = 0; i < OriginTrail.plr.getParty().length; i++)
                                if(i < rem)
                                        temp[i] = OriginTrail.plr.getParty()[i];
                                else if(i > rem)
                                        temp[i-1] = OriginTrail.plr.getParty()[i];
                        ModuleMove.renote(OriginTrail.plr.getParty()[rem].getName() + " drowned.");
                        OriginTrail.plr.setParty(temp);
                }else if(test == 99){}
                OriginTrail.plr.getMap().moveOne();
                ModuleMove.redoLabels();
                parent.dispose();
            }else if(e.getSource().equals(goAround)){
                if(OriginTrail.plr.bags.find(new Food()).stack >= 5*OriginTrail.plr.getFoodSpeed().foodCost + 1){
                        OriginTrail.plr.bags.addItem(new Food().initialize((int)(-5*OriginTrail.plr.getFoodSpeed().foodCost)));
                        OriginTrail.plr.getMap().moveOne();
                        ModuleMove.redoLabels();
                        parent.dispose();
                }else{
                    inst.setVisible(false);
                    inst.setText("Not enough food");
                    inst.setVisible(true);
                    parent.pack();
                }
                
            }
        }
    }
}
